package com.dodoke.stuMS;

import com.dodoke.ArrayList.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("001", "刘备", 30, "1922-06-06"));
        students.add(new Student("002", "关羽", 30, "1922-06-06"));
        students.add(new Student("003", "张飞", 30, "1922-06-06"));
        do {


            System.out.println("1.添加学生\n" +
                    "2.删除学生\n" +
                    "3.修改学生\n" +
                    "4.查看学生\n" +
                    "5.退出系统 ");
            System.out.print("请选择功能：");
            int choose = in.nextInt();
            switch (choose) {
                case 1:
                    System.out.println("添加学生");
                    boolean r = addStu(students);
                    System.out.println(r ? "添加成功" : "添加失败");
                    break;
                case 2:
                    System.out.println("删除学生");
                    break;
                case 3:
                    System.out.println("修改学生");
                    updateStudent(students);
                    break;
                case 4:
                    System.out.println("查看学生");
                    show(students);
                    break;
                case 5:
                    System.out.println("退出系统");
                    return;
            }
        } while (true);

    }

    public static boolean addStu(ArrayList<Student> students) {
        Scanner in = new Scanner(System.in);

        String sid;
        while (true) {
            System.out.print("请输入学号：");
            String id = in.next();
            int index = sidInList(students, id);
            if (index == -1) {
                sid = id;
                break;
            } else {
                System.err.println("学号已存在重新输入：");
            }
        }


        System.out.print("请输入姓名：");
        String name = in.next();
        System.out.print("请输入年龄：");
        int age = in.nextInt();
        System.out.print("请输入生日：");
        String birthday = in.next();
        Student stu = new Student(sid, name, age, birthday);
        boolean f =students.add(stu);
        return f;
    }

    public static void show(ArrayList<Student> students) {
        for (Student item : students) {
            System.out.println(item);
        }
    }

    public static int sidInList(ArrayList<Student> students, String sid) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getSid().equalsIgnoreCase(sid)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public static boolean delStudent(ArrayList<Student> students) {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入学生的编号： ");
        String sid = in.next();
        int index = sidInList(students, sid);
        if (index == -1) {
            System.err.println("删除失败，编号不存在");
            return false;
        } else {
            Student stu = students.remove(index);
            return true;
        }
    }
    public static void updateStudent(ArrayList<Student> array) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要修改学生的学号：");
        String sid=sc.nextLine();
        System.out.println("请输入学生新姓名：");
        String name=sc.nextLine();
        System.out.println("请输入学生新年龄：");
        int age= Integer.parseInt(sc.nextLine());
        System.out.println("请输入学生新生日：");
        String birthday=sc.nextLine();
        Student s=new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setBirthday(birthday);
        for(int i=0;i<array.size();i++) {
            Student student=array.get(i);
            if(student.getSid().equals(sid)) {
                array.set(i,s);
                break;
            }
        }
        System.out.println("修改学生成功");
    }
}

